package com.housekeeping.housekeeping.wxPay.controller;

import com.housekeeping.housekeeping.serviceItem.utils.RequestUtils;
import com.housekeeping.housekeeping.serviceItem.utils.ResponseUtils;
import com.housekeeping.housekeeping.serviceItem.utils.StatUtil;
import com.housekeeping.housekeeping.wxPay.config.PayConfiguration;
import com.housekeeping.housekeeping.wxPay.utils.PayHelper;
import com.housekeeping.housekeeping.wxPay.utils.PayState;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/PayServlet/query")
public class WXPayQueryStatusController extends HttpServlet {
    private PayHelper payHelper=new PayHelper(new PayConfiguration());
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = RequestUtils.verify(req);
        String oid = req.getParameter("order");
        aid="4399";
        //登录校验
        if (aid==null){ ResponseUtils.fail(resp, StatUtil.NEED_LOGIN); }
        try {
            PayState payState = payHelper.queryOrder(Long.parseLong(oid));
            ResponseUtils.success(resp,payState.getValue(),StatUtil.SUCCESS);
        }catch (NumberFormatException formatException)
        {
            ResponseUtils.fail(resp,StatUtil.INTERNET_ERR);
        }
        catch (Exception e)
        {
            ResponseUtils.fail(resp,StatUtil.INTERNET_ERR);
            e.printStackTrace();
        }
    }
}
